package com.yy.controller.main;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.page.TableDataInfo;
import com.yy.pojo.Survey;
import com.yy.pojo.SurveyItem;
import com.yy.pojo.SurveySubmit;
import com.yy.service.SurveyItemService;
import com.yy.service.SurveyService;
import com.yy.service.SurveySubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SurveyController
 * Package: com.yy.controller.main
 * Description:
 *
 * @Author yy
 * @Create 2023-12-28 15:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/main/survey")
public class SurveyController {

    @Autowired
    private SurveyService surveyService;

    @Autowired
    private SurveyItemService surveyItemService;

    @Autowired
    private SurveySubmitService surveySubmitService;

    // 查询全部调查
    @GetMapping("/query")
    public TableDataInfo query(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               Survey survey) {
        IPage<Survey> page = surveyService.getSurveyList(pageNum, pageSize, survey);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    // 查询某调查的所有子项，参数String(bigint)
    @GetMapping("{id}")
    public AjaxResult querySurveyItem(@PathVariable("id") Long id) {
        List<SurveyItem> surveyItemList = surveyItemService.list(Wrappers.lambdaQuery(SurveyItem.class).eq(SurveyItem::getSurveyId, id));
        return AjaxResult.success(surveyItemList);
    }

    // 参数：SurveySubmit列表
    @PostMapping("/add")
    public AjaxResult add(@RequestBody List<SurveySubmit> surveySubmits) {
        surveySubmits.stream().forEach(e -> e.setCreateTime(DateUtil.date()));
        if (surveySubmitService.saveBatch(surveySubmits)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
